//
//  Asteroid.m
//  MathMastersAsteroids
//
//  Created by Jordon Phillips on 3/7/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Asteroid.h"


@implementation Asteroid

@synthesize number, health, speed, explode, soundToggle;

//initilizes the asteroid to the proper starting values
- (void)initAsteroid:(float)newX:(float)newY:(int)newValue{
	sound =[SoundController alloc];
	[sound initSoundController];
	[sound addSound:@"boom" filetype:@"wav"];
	self.health = 100;
	self.xPos = newX;
	self.yPos = newY;
	self.sprite = [[Sprite alloc] init];
	self.sprite.xPos = self.xPos;
	self.sprite.yPos = self.yPos;
	self.sprite.scale = 40;
	self.sprite.image = (2);
	self.number = [[Number alloc]init];
	self.sprite.dead = FALSE;
	[self.sprite setDimensions:5];
	self.number.sprite.dead = FALSE;
	self.speed = 0.1;
	self.explode= FALSE;
	self.sprite.currentFrame=arc4random()%10;
	[self.number initNumber:newX :newY :newValue];
	animWait=0;
	animation = [NSTimer scheduledTimerWithTimeInterval:.05 target:self selector:@selector(animate) userInfo:nil repeats:YES];

}
//what the asteroid does every frame
- (void)act{
	if(!number.explode){
		[self moveX:self.speed];
		[self.number moveX:self.speed];
	}
	if(self.xPos>=155){
		self.sprite.dead=TRUE;
		self.number.sprite.dead=TRUE;
	}
}
//what happens to the asteroid if it hits the player
- (void)collideWithPlayer{
	if(self.sprite.dead||self.explode){
		if(!self.number.sprite.dead&&!number.explode){
			self.number.sprite.dead=TRUE;
			NSLog(@"Collected number: %i",self.number.value);
		}				
	}else{
		if(self.health<=0){
			if(!self.sprite.dead&&!self.explode)
				if(soundToggle)
					[sound playSound:0];
			self.explode=TRUE;
			NSLog(@"Asteroid Dead");
		}else{
			self.health-=50;
			NSLog(@"Asteroid Health: %i",self.health);
		}
	}
}
//what happens to the asteroid if it hits a bullet
- (void)collideWithBullet{
	if(self.health<=-100){
		self.number.explosion.xPos=self.xPos;
		self.number.explosion.yPos=self.yPos;
		self.number.explosion.currentFrame=1;
		if(soundToggle)
			[sound playSound:0];
		self.number.sprite.dead=TRUE;
		self.number.explode=TRUE;
		NSLog(@"Number Dead");
	}else if(self.health<=0){
		if(!self.sprite.dead&&!self.explode)
			if(soundToggle)
				[sound playSound:0];
		self.explode=TRUE;
		self.health-=10;
		NSLog(@"Asteroid Dead");
	}else{
		self.health-=35;
		NSLog(@"Asteroid Health: %i",self.health);
	}
	
}
//reinitilizes the asteroid for reuse
- (void)reset:(float)newX:(float)newY:(int)newValue{
	self.health = 100;
	self.sprite.dead = FALSE;
	self.number.sprite.dead = FALSE;
	self.xPos = newX;
	self.yPos = newY;
	self.sprite.xPos = self.xPos;
	self.sprite.yPos = self.yPos;
	self.number.xPos = self.xPos;
	self.number.yPos = self.yPos;
	self.number.sprite.xPos = self.xPos;
	self.number.sprite.yPos = self.yPos;
	self.sprite.scale = 40;
	self.sprite.image = (2);
	self.number.value = newValue;
	self.number.sprite.image = (3+newValue);
	self.sprite.currentFrame=arc4random()%10;
	self.number.explosion.dead=FALSE;
	self.number.explosion.currentFrame=0;
	self.number.explode=FALSE;
	
}
//changes the frame of the current animation
-(void)animate{
	if(number.explode){
		number.explosion.currentFrame++;
		if(number.explosion.currentFrame>=9){
			number.explosion.dead=TRUE;
			number.sprite.dead=TRUE;
			number.explode=FALSE;
		}
	}
	if(self.explode){
		if(self.sprite.currentFrame<10)
			self.sprite.currentFrame=9;
		self.sprite.currentFrame++;
		if(self.sprite.currentFrame>=24){
			self.sprite.dead=TRUE;
			self.explode=FALSE;
		}
	}else{
		if(animWait>=2){
			self.sprite.currentFrame++;
			animWait=0;
		}else
			animWait++;
		if(self.sprite.currentFrame>=9){
			self.sprite.currentFrame=0;
		}
	}
}
-(void) dealloc{
	[number release];
	[sound release];
	[animation release];
	[super dealloc];
}
@end
